<?php

function findObjectById($orgid,$prodname){
	echo "Test";
}

/* CATEGORY */
function getCategories($method_name, $args, $app_data){
	$orgid=$args[0];
	$query="select * from categories where orgid=".$orgid;
	$result=mysql_query($query);
	$num=mysql_numrows($result);
	$resultarray=array();
	$i=0;
	while ($i < $num) {

		$resultarray[]=array(
		'id'=>mysql_result($result,$i,"id"),
		'name'=>mysql_result($result,$i,"name")
		);
		$i++;
	}
	return $resultarray;
}

function createCategory($method_name,$args,$app_data){
	$orgid=$args[0];
	$name=$args[1];
	$query="insert into categories (orgid,name) values (".$orgid.",'".$name."')";
	$result=mysql_query($query);
	return $args;
}

function updateCategory($method_name,$args,$app_data){
	$orgid=$args[0];
	$catid=$args[1];
	$name=$args[2];
	$query="update categories set name='".$name."' where orgid=".$orgid." and id=".$catid;
	$result=mysql_query($query);
	return $args;
}

function deleteCategory($method_name,$args,$app_data){
	$orgid=$args[0];
	$catid=$args[1];
	$query="delete from categories where orgid=".$orgid." and id=".$catid;
	$result=mysql_query($query);
	return array('success'=>1);
}

/* CATEGORY ENDS */


/* PRODUCT STARTS */

function getAllProducts($method_name, $args, $app_data){
	$orgid=$args[0];

	$query="select * from products o inner join categories c on c.id=o.catid where o.orgid=".$orgid;
	$result=mysql_query($query);
	$num=mysql_numrows($result);
	$resultarray=array();
	$i=0;
	while ($i < $num) {

		$resultarray[]=array(
			'id'=>mysql_result($result,$i,"objid"),
			'name'=>mysql_result($result,$i,"prodname"),
			'catid'=>mysql_result($result,$i,"catid"),
			'catname'=>mysql_result($result,$i,"name")
		);
		$i++;
	}

	return $resultarray;
}

function createProduct($method_name, $args, $app_data){
	$orgid=$args[0];
	$name=$args[1];
	$catid=$args[2];
	$query="insert into products (orgid,prodname,catid) values (".$orgid.",'".$name."',".$catid.")";
	mysql_query($query);
	$insertid=mysql_insert_id();

	$query1="insert into product_attributes(prod_id, attr_id, default_value, activeind,orgid) VALUES".
			" (".$insertid.",1,'id of the product',1,".$orgid.")";
	mysql_query($query1);
	$query2="insert into product_attributes(prod_id, attr_id, default_value, activeind,orgid) VALUES".
				" (".$insertid.",2,'Name of the product',1,".$orgid.")";
	mysql_query($query2);
	$query3="insert into product_attributes(prod_id, attr_id, default_value, activeind,orgid) VALUES".
				" (".$insertid.",3,'Price of the product',1,".$orgid.")";
	mysql_query($query3);

	$query4="insert into product_attributes(prod_id, attr_id, default_value, activeind,orgid) VALUES".
					" (".$insertid.",4,'Quantity available',1,".$orgid.")";
	mysql_query($query4);
	return array('success'=>1);
}

function updateProduct($method_name, $args, $app_data){
	$orgid=$args[0];
	$objid=$args[1];
	$name=$args[2];
	$catid=$args[3];
	$query="update products set prodname='".$name."', catid=".$catid." where orgid=".$orgid." and objid=".$objid;
	$result=mysql_query($query);
	return $result;
}

function deleteProduct($method_name, $args, $app_data){
	$orgid=$args[0];
	$objid=$args[1];
	$query="delete from products where orgid=".$orgid." and objid=".$objid;
	$result=mysql_query($query);

	return $query;
}

/* END PRODUCTS */

/* PRODUCT ATTRIBUTES */

function getAllProdAttributes($method_name, $args, $app_data){
	$orgid=$args[0];
	$prodid=$args[1];
	$query="select * from product_attributes p inner join attributes on  id=attr_id where p.orgid=".$orgid." and prod_id=".$prodid;
	$result=mysql_query($query);
	$num=mysql_numrows($result);
	$resultarray=array();
	$i=0;
	while ($i < $num) {
		$resultarray[]=array(
				'id'=>mysql_result($result,$i,"prod_attr_id"),
 				'prodid'=>mysql_result($result,$i,"prod_id"),
				'attrid'=>mysql_result($result,$i,"attr_id"),
				'orgid'=>mysql_result($result,$i,"orgid"),
				'defaultvalue'=>mysql_result($result,$i,"default_value"),
				'activeind'=>mysql_result($result,$i,"activeind"),
				'attribute_name'=>mysql_result($result,$i,"attribute_name")

		);
		$i++;
	}
	return $resultarray;
}

function createProdAttribute($method_name, $args, $app_data){
	$orgid=$args[0];
	$defaultvalue=$args[1];
	$fieldname=$args[2];
	$objid=$args[3];
	$query="insert into attributes (orgid,attribute_name) values (".$orgid.",'".$fieldname."')";
	mysql_query($query);
	$attr_id=mysql_insert_id();
	if($attr_id > 0){
		$query1="insert into product_attributes (orgid,prod_id,attr_id,default_value) values (".$orgid.",".$objid.",".$attr_id.",'".$defaultvalue."')";
		mysql_query($query1);
		return array('success'=>1,'pa'=>1);
	}
	return array('success'=>1);

}

function updateProdAttribute($method_name, $args, $app_data){
	$orgid=$args[0];
	$prod_attr_id=$args[1];
	$defaultvalue=$args[2];
	$fieldname=$args[3];
	$objid=$args[4];
	$attrid=$args[5];
	$query="update attributes set attribute_name='".$fieldname."' where id=".$attrid." and orgid=".$orgid;
	$query1="update product_attributes set default_value='".$defaultvalue."' where 	prod_attr_id=".$prod_attr_id;
	mysql_query($query);
	mysql_query($query1);
	return array('success'=>1);

}

function deleteProdAttribute($method_name, $args, $app_data){
	$orgid=$args[0];
	$attrid=$args[1];
	$query="delete from attributes  where id=".$attrid." and orgid=".$orgid;
	mysql_query($query);

	return array('success'=>1);

}
/* END PRODUCT ATTRIBUTES */



/* PRODUCT CATALOG ITEMS */
function getAllItems($method_name, $args, $app_data){
	$orgid=$args[0];
	$objid=$args[1];
	$result=mysql_query("Select distinct item_unique_key from items_attributes where objid=".$objid." and orgid=".$orgid);
	$num=mysql_numrows($result);
	$keysarray=array();
	$i=0;
	while ($i < $num) {

		$keysarray[]=mysql_result($result,$i,"item_unique_key");
		$i++;
	}
	$resultarray=array();
	foreach($keysarray as $itemkey){
		$query="SELECT a.attribute_name, i.item_value, i.item_unique_key".
			   " FROM items_attributes AS i".
			   " INNER JOIN product_attributes AS p ON p.prod_attr_id = i.prod_attr_id".
			   " INNER JOIN attributes AS a ON a.id = p.attr_id".
			   " WHERE i.orgid=".$orgid." and i.objid=".$objid." and i.item_unique_key='".$itemkey."'".
			   " ORDER BY i.item_unique_key";
		$result=mysql_query($query);
		$num=mysql_numrows($result);
		$itemarray=array();
		while($row = mysql_fetch_array($result))
		{
			$itemarray[$row[0]]=$row[1];
		}
		$resultarray[]=$itemarray;
	}
	return $resultarray;
}

function createItem($method_name, $args, $app_data){
	$orgid=$args[0];
	$objid=$args[1];
	$jsonstring=$args[2];
	$attrarray=json_decode($jsonstring);
	$query="select prodname from products where objid=".$objid;
	$result=mysql_query($query);

	$prodname=str_replace(' ', '', mysql_result($result,0,"ObjName"));

	$result=mysql_query("Select distinct item_unique_key from items_attributes where objid=".$objid." and orgid=".$orgid);
	$num=mysql_numrows($result);

	$item_unique_key=$prodname.($num+1);

	$text="123";
	foreach($attrarray as $key=>$value){

		$query="insert into items_attributes(objid, prod_attr_id, item_unique_key, item_value, activeind, orgid) VALUES (".$objid.",'".$key."','".$item_unique_key."','".$value."','1',".$orgid.")";
		mysql_query($query);
	}

	return $item_unique_key;
}

function deleteItem($method_name, $args, $app_data){
	$orgid=$args[0];
	$item_attr_id=$args[1];
	$item_unique_key=$args[2];
	$query="delete from items_attributes where orgid=".$orgid." and item_unique_key=".$item_unique_key;
	$result=mysql_query($query);
	return array('success'=>1);
}

function updateItem($method_name, $args, $app_data){
	$orgid=$args[0];
	$item_attr_id=$args[1];
	$item_value=$args[2];
	$active=$args[3];

	$query="update items_attributes set item_value='".$item_value."', activeind=".$active." where orgid=".$orgid." and item_attr_id=".$item_attr_id;
	$result=mysql_query($query);
	return $result;

}

/* END PRODUCT CATALOG ITEMS */



/* CUSTOM OBJECTS */

function getAllCustObjs($method_name, $args, $app_data){
	$orgid=$args[0];
	$query="select * from cobjects  where orgid=".$orgid;
	$result=mysql_query($query);
	$num=mysql_numrows($result);
	$resultarray=array();
	$i=0;
	while ($i < $num) {
		$resultarray[]=array(
			'id'=>mysql_result($result,$i,"objid"),
			'name'=>mysql_result($result,$i,"objname"),
			'descr'=>mysql_result($result,$i,"objdescr")
		);
		$i++;
	}
	return $resultarray;
}

function createCustObj($method_name, $args, $app_data){
	$orgid=$args[0];
	$name=$args[1];
	$query="insert into cobjects (orgid,objname) values (".$orgid.",'".$name."')";
	mysql_query($query);
	$insertid=mysql_insert_id();
	return $insertid;
}

function updateCustObj($method_name, $args, $app_data){
	$orgid=$args[0];
	$objid=$args[1];
	$name=$args[2];
	$query="update cobjects set objname='".$name."' where orgid=".$orgid." and objid=".$objid;
	$result=mysql_query($query);
	return $result;
}

function deleteCustObj($method_name, $args, $app_data){
	$orgid=$args[0];
	$objid=$args[1];
	$query="delete from cobjects where orgid=".$orgid." and objid=".$objid;
	$result=mysql_query($query);
	return $query;
}

/* END CUSTOM OBJECTS */


/* CUSTOM ATTRIBUTES */

function getCustAttributes($method_name, $args, $app_data){
	$orgid=$args[0];
	$objid=$args[1];
	$query="select * from cust_attributes where orgid=".$orgid." and cobjid=".$objid;
	$result=mysql_query($query);
	$num=mysql_numrows($result);
	$resultarray=array();
	$i=0;
	while ($i < $num) {

		$resultarray[]=array(
				'cattrid'=>mysql_result($result,$i,"cattrid"),
				'orgid'=>mysql_result($result,$i,"orgid"),
				'cobjid'=>mysql_result($result,$i,"cobjid"),
				'fieldname'=>mysql_result($result,$i,"name"),
				'datatype'=>mysql_result($result,$i,"datatype"),
				'defaultvalue'=>mysql_result($result,$i,"defaultvalue"),
				'fieldnum'=>mysql_result($result,$i,"fieldnu"),
				'activeind'=>mysql_result($result,$i,"activeind"),

		);
		$i++;
	}
	return $resultarray;
}

function createCustAttr($method_name, $args, $app_data){
	$orgid=$args[0];
	$objid=$args[1];
	$fieldname=$args[2];
	$defaultvalue=$args[3];
	$query="insert into cust_attributes (orgid, cobjid, name, defaultvalue)".
	"VALUES (".$orgid.",".$objid.",'".$fieldname."','".$defaultvalue."')";
	mysql_query($query);
	return $query;
}

function updateCustAttr($method_name, $args, $app_data){
	$orgid=$args[0];
	$fieldid=$args[1];
	$fieldname=$args[2];
	$defaultvalue=$args[3];
	mysql_query("update cust_attributes set name='".$fieldname."' where orgid=".$orgid." and cattrid=".$fieldid);
	mysql_query("update cust_attributes set defaultvalue='".$defaultvalue."' where orgid=".$orgid." and cattrid=".$fieldid);
	return array('success'=>1);
}


function deleteCustAttr($method_name, $args, $app_data){
	$orgid=$args[0];
	$cattrid=$args[1];
	$query="delete from cust_attributes where orgid=".$orgid." and cattrid=".$cattrid;
	$result=mysql_query($query);
	return $query;
}

/* END CUSTOM ATTRIBUTES */


/* CUSTOM OBJECTS DATA */
function getAllCustItems($method_name, $args, $app_data){
	$orgid=$args[0];
	$objid=$args[1];
	$result=mysql_query("Select distinct citem_unique_key from citems_attributes where cobjid=".$objid." and orgid=".$orgid);
	$num=mysql_numrows($result);
	$keysarray=array();
	$i=0;
	while ($i < $num) {

		$keysarray[]=mysql_result($result,$i,"citem_unique_key");
		$i++;
	}
	
	// 
	
	$resultarray=array();
	foreach($keysarray as $itemkey){
		$query="SELECT name, i.citem_value, i.citem_unique_key".
			   " FROM  citems_attributes AS i".
			   " INNER JOIN cust_attributes AS p ON p.cattrid = i.cobj_attr_id".
			   " WHERE i.orgid=".$orgid." and i.cobjid=".$objid." and i.citem_unique_key='".$itemkey."'".
			   " ORDER BY i.citem_unique_key";
		$result=mysql_query($query);
		$num=mysql_numrows($result);
		$itemarray=array();
		while($row = mysql_fetch_array($result))
		{
			$itemarray[$row[0]]=$row[1];
		}
		$resultarray[]=$itemarray;
	}
	return $resultarray;
}

function createCustItem($method_name, $args, $app_data){
	$orgid=$args[0];
	$objid=$args[1];
	$jsonstring=$args[2];
	$attrarray=json_decode($jsonstring);
	$query="select objname from cobjects where objid=".$objid;
	$result=mysql_query($query);

	$objname=str_replace(' ', '', mysql_result($result,0,"objname"));

	$result=mysql_query("Select distinct citem_unique_key from citems_attributes where cobjid=".$objid." and orgid=".$orgid);
	$num=mysql_numrows($result);

    $item_unique_key=$objname.($num+1);

	foreach($attrarray as $key=>$value){
		$query="insert into citems_attributes (cobjid, cobj_attr_id, citem_unique_key, citem_value, activeind, orgid) VALUES (".$objid.",".$key.",'".$item_unique_key."','".$value."',1,".$orgid.")";
	    mysql_query($query);
	}

	return $item_unique_key;
}

function deleteCustItem($method_name, $args, $app_data){
	$orgid=$args[0];
	$citem_attr_id=$args[1];
	$citem_unique_key=$args[2];
	$query="delete from citems_attributes where orgid=".$orgid." and citem_unique_key=".$item_unique_key;
	$result=mysql_query($query);
	return array('success'=>1);
}

function updateCustItem($method_name, $args, $app_data){
	$orgid=$args[0];
	$item_attr_id=$args[1];
	$citem_value=$args[2];
	$active=$args[3];

	$query="update citems_attributes set citem_value='".$item_value."', activeind=".$active." where orgid=".$orgid." and citem_attr_id=".$citem_attr_id;
	$result=mysql_query($query);
	return $result;

}

/* END CUSTOM OBJECTS DATA */



?>
